package com.oa.ylms.apps.actions.archives;

import java.util.List;

import com.oa.ylms.apps.actions.BasicAction;
import com.oa.ylms.apps.business.archives.IStaffInfoService;
import com.oa.ylms.apps.business.organization.IPostInfoService;
import com.oa.ylms.apps.business.organization.ISectionInfoService;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.organization.PostInfoValue;
import com.oa.ylms.apps.entity.organization.SectionInfoValue;

@SuppressWarnings("serial")
public class StaffInfoAction extends BasicAction {
	
	private IStaffInfoService staffService;
	private ISectionInfoService sectionService;
	private StaffInfoValue staffInfo;
	private List<StaffInfoValue>staffList;
	private List<SectionInfoValue>sectionList;
	private List<PostInfoValue> postList;
	private IPostInfoService postService;
	
	private Integer staffId;
	private String staffIds;
	private Integer sectionId;
	private String staffNo;
	private String staffPwd;
	private Integer state;
	private Integer postId;
	private String staffName;
	private String searchStaffName;
	private String searchStaffNo;
	
	private String str;
	
	@Override
	public String execute() throws Exception {
		if("".equals(searchStaffName)||searchStaffName == null)
			staffName = null;
		else{
			staffName = searchStaffName;
		}
		if("".equals(searchStaffNo) || searchStaffNo==null)
			staffNo = null;
		else{
			staffNo = searchStaffNo;
		}
		totalpage=staffService.countTPage(staffNo,postId, staffName,state, rows);
		count=staffService.getTotalNumber();
		if(count%rows==0&&totalpage+1==currpage){
			currpage=totalpage;
		}
		sectionList = sectionService.querySectionInfo(null, IDEDStates.ENABLE);
		staffList=	staffService.queryStaffInfoWithPage(staffNo,postId, staffName,state, rows, currpage);
		if(sectionId != null)
			postList = postService.queryPostInfoBySectionId(sectionId);
		return SUCCESS;
	}
	
	public String checkStaff() throws Exception {
		staffInfo = staffService.verifyStaffInfo(staffNo, staffPwd);
	    String result = "";
		if(staffInfo != null){
			session.put("login_staffInfo", staffInfo);
			result = "ok";
		}else 
			result = "no"; 
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().write(result);
		response.flushBuffer();
		return null;
	}
	
	public String queryPostBySection() throws Exception {
		if (sectionId != null) {
			postList = postService.queryPostInfoBySectionId(sectionId);
			response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			this.str = getPostOptionsFrom(postList); // obj.toString();
			response.getWriter().print(str);
		}
		return null;
	}

	public String getPostOptionsFrom(List<PostInfoValue> list) {
		StringBuilder sb = new StringBuilder();
		sb.append("<option value=\"\">-所有-</option>");
		for (PostInfoValue piv : list) {
			String temp = "<option value=\"" + piv.getPostId().toString()
					+ "\">" + piv.getPostName() + "</option>";
			sb.append(temp);
		}
		return sb.toString();
	}
	
	public String delete() throws Exception{
		staffInfo = (StaffInfoValue)session.get("login_staffInfo");
		if(staffService.deleteStaffInfo(staffIds,staffInfo)){
			return "delete";
		}
		return "false";
	}
	
	public String toDisable() throws Exception{
		staffInfo = (StaffInfoValue)session.get("login_staffInfo");
		if(staffService.toDisable(staffId,staffInfo)){
			return "toDisable";
		}
		return "false";
	}
	
	public String toEnable() throws Exception{
		staffInfo = (StaffInfoValue)session.get("login_staffInfo");
		if(staffService.toEnable(staffId,staffInfo)){
			return "toEnable";
		}
		return "false";
	}
	
	
	//
	public void setStaffService(IStaffInfoService staffService) {
		this.staffService = staffService;
	}

	public IStaffInfoService getStaffService() {
		return staffService;
	}

	public String getStaffNo() {
		return staffNo;
	}

	public void setStaffNo(String staffNo) {
		this.staffNo = staffNo;
	}

	public String getStaffPwd() {
		return staffPwd;
	}

	public void setStaffPwd(String staffPwd) {
		this.staffPwd = staffPwd;
	}

	public void setStaffInfo(StaffInfoValue staffInfo) {
		this.staffInfo = staffInfo;
	}

	public StaffInfoValue getStaffInfo() {
		return staffInfo;
	}

	public List<StaffInfoValue> getStaffList() {
		return staffList;
	}

	public void setStaffList(List<StaffInfoValue> staffList) {
		this.staffList = staffList;
	}

	public Integer getPostId() {
		return postId;
	}

	public void setPostId(Integer postId) {
		this.postId = postId;
	}

	public String getStaffName() {
		return staffName;
	}

	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}

	public ISectionInfoService getSectionService() {
		return sectionService;
	}

	public void setSectionService(ISectionInfoService sectionService) {
		this.sectionService = sectionService;
	}

	public List<SectionInfoValue> getSectionList() {
		return sectionList;
	}

	public void setSectionList(List<SectionInfoValue> sectionList) {
		this.sectionList = sectionList;
	}

	public List<PostInfoValue> getPostList() {
		return postList;
	}

	public void setPostList(List<PostInfoValue> postList) {
		this.postList = postList;
	}

	public IPostInfoService getPostService() {
		return postService;
	}

	public void setPostService(IPostInfoService postService) {
		this.postService = postService;
	}

	public Integer getSectionId() {
		return sectionId;
	}

	public void setSectionId(Integer sectionId) {
		this.sectionId = sectionId;
	}

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	public void setSearchStaffName(String searchStaffName) {
		this.searchStaffName = searchStaffName;
	}

	public String getSearchStaffName() {
		return searchStaffName;
	}

	public void setSearchStaffNo(String searchStaffNo) {
		this.searchStaffNo = searchStaffNo;
	}

	public String getSearchStaffNo() {
		return searchStaffNo;
	}

	public void setStaffId(Integer staffId) {
		this.staffId = staffId;
	}

	public Integer getStaffId() {
		return staffId;
	}

	public void setStaffIds(String staffIds) {
		this.staffIds = staffIds;
	}

	public String getStaffIds() {
		return staffIds;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	public Integer getState() {
		return state;
	}
}